var calendarUtil = {
    baseUrl: undefined,
    redirectBaseUrl: undefined,
    tooltip: function (target) {
        /* CONFIG */
        xOffset = 10;
        yOffset = 20;
        // these 2 variable determine popup's distance from the cursor
        // you might want to adjust to get the right result
        /* END CONFIG */
        target.hover(function (e) {
            this.t = $(this).attr("tt");
            $("body").append("<p id='tooltip'>" + this.t + "</p>");
            $("#tooltip").css("top", (e.pageY - xOffset) + "px").css("left", (e.pageX + yOffset) + "px").fadeIn("fast");
        }, function () {
            $("#tooltip").remove();
        }).mousemove(function (e) {
            $("#tooltip").css("top", (e.pageY - xOffset) + "px").css("left", (e.pageX + yOffset) + "px");
        });
    },
    switchToolbar: function (toolbarInstance, isEdit) {
        if (toolbarInstance && toolbarInstance.hasClass("toolbar")) {
            var year = toolbarInstance.find(".year");
            var month = toolbarInstance.find(".month");
            var yearValue = this.getValue(year);
            var monthValue = this.getValue(month);
            if (isEdit) {
                var control = $("<select></select>");
                control.addClass("year");
                for (var i = yearValue * 1 - 5; i <= yearValue * 1 + 5; i++) {
                    var option = $("<option value='" + i + "'>" + i.toString() + "</option>");
                    if (yearValue == i.toString()) {
                        option.attr("selected", true);
                    }
                    control.append(option);
                }
                year.remove();
                control.bind("change", function (e) {
                    var year = $(this).parent().children(".year").val();
                    var month = $(this).parent().children(".month").val();
                    calendarUtil.viewCalendar($(this), year, month);
                });
                toolbarInstance.find(".current-month").append(control);

                var control = $("<select></select>");
                control.addClass("month");
                for (var i = 1; i <= 12; i++) {
                    var option = $("<option value='" + i + "'>" + this.getMonthWord(i) + "</option>");
                    if (monthValue == i.toString()) {
                        option.attr("selected", true);
                    }
                    control.append(option);
                }
                month.remove();
                control.bind("change", function (e) {
                    var year = $(this).parent().children(".year").val();
                    var month = $(this).parent().children(".month").val();
                    calendarUtil.viewCalendar($(this), year, month);
                });
                toolbarInstance.find(".current-month").append(control);
            } else {
                var span = $("<span></span>");
                span.addClass("year");
                span.text(yearValue);
                span.attr("value", yearValue);
                year.remove();
                toolbarInstance.find(".current-month").append(span);

                span = $("<span></span>");
                span.addClass("month");
                span.text(this.getMonthWord(monthValue));
                span.attr("value", monthValue);
                month.remove();
                toolbarInstance.find(".current-month").append(span);
            }
        }
    },
    getUrl: function (year, month, baseUrl) {
        if (!baseUrl) {
            baseUrl = this.baseUrl || window.location.href;
        }
        if (baseUrl.indexOf("?") < 0) {
            baseUrl = baseUrl + "?";
        }
        return baseUrl + 'year=' + year + '&month=' + month;
    },
    redirectToCalendar: function (year, month, redirectBaseUrl) {
        var url = this.getUrl(year, month, redirectBaseUrl || this.redirectBaseUrl);
        window.location.href = url;
    },
    viewCalendar: function (caller, year, month, baseUrl) {
        var url = this.getUrl(year, month, baseUrl);
        window.location.href = url;
    },
    viewCalendarAjax: function (caller, year, month, baseUrl) {
        var container = caller.data("container");
        if (container) {
            var url = this.getUrl(year, month, baseUrl);
            var mask = $.win.progress.show(container);
            $.ajax({
                url: url,
                type: 'GET',
                contentType: 'application/json',
                async: true,
                success: function (result) {
                    if (result) {
                        container.children().remove();
                        container.append(result);
                        calendarUtil.initNavForThumbnail(container.find("div.calendar"));
                    }

                    $.win.progress.hide(mask);
                }
            });
        }
    },
    initNavForThumbnail: function (calendar) {
        calendar.find("div.has-event").each(function (j, currrent) {
            calendarUtil.tooltip($(currrent));
        });
        var container = calendar.parent().parent();
        var toolbar = calendar.children("div.toolbar");
        calendarUtil.switchToolbar(toolbar, false);
        toolbar.children().data("container", container);
    },
    getValue: function (element) {
        if (element[0].nodeName.toLowerCase() == 'span') {
            return element.attr("value") || element.text();
        } else {
            return element.val();
        }
    },
    getMonthWord: function (monthValue) {
        var result = "";
        switch (monthValue.toString()) {
            case "1":
                result = "January";
                break;
            case "2":
                result = "February";
                break;
            case "3":
                result = "March";
                break;
            case "4":
                result = "April";
                break;
            case "5":
                result = "May";
                break;
            case "6":
                result = "June";
                break;
            case "7":
                result = "July";
                break;
            case "8":
                result = "August";
                break;
            case "9":
                result = "September";
                break;
            case "10":
                result = "October";
                break;
            case "11":
                result = "November";
                break;
            case "12":
                result = "December";
                break;
            default:
                break;
        }
        return result;
    }
}

$(document).ready(function () {
    $("div.calendar").each(function (i, c) {
        if ($(c).hasClass("eventsummary")) {
            var toolbar = $(c).children("div.toolbar");
            toolbar.bind("mouseenter", function () {
                calendarUtil.switchToolbar($(this), true);
            }).bind("mouseleave", function () {
                calendarUtil.switchToolbar($(this), false);
            });
            calendarUtil.switchToolbar(toolbar, false);
        }
        else if ($(c).hasClass("eventinhover")) {

            calendarUtil.initNavForThumbnail($(c));
        }
    });
});